package br.ufpb.di.knowledgetv.sqtv.modules;

import br.ufpb.di.knowledgetv.algorithms.LetterPairSimilarity;
import br.ufpb.di.knowledgetv.sqtv.utils.Data;
import br.ufpb.di.knowledgetv.sqtv.utils.Result;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

import java.util.ArrayList;
import java.util.List;

/**
 * User: manoel
 * Date: 29/04/13
 */
@Component
public class ProcessResponseModule implements SQTVModule {
    @Override
    public Data execute(Data data) {
        List<Result> results = new ArrayList<Result>();

    	if (data.hasData("local_results")) {
    		ResultSet resultSet = (ResultSet) data.getData("local_results");
            while (resultSet.hasNext()) {
                QuerySolution qs = resultSet.next();
                Result result = new Result();
                result.put("uri", qs.getResource("thing").getURI());
                result.put("label", qs.getLiteral("label").getValue());
                result.put("abstract", qs.getLiteral("abstract").getValue());
                result.put("type", qs.getResource("type").getLocalName());
                result.put("weight", LetterPairSimilarity.compareStrings( (String)result.get("label"), (String) data.getData("keywords")));

                results.add(result);
            }
    	} else if (data.hasData("result_model")) {

        }


        data.addData("final_result", results);
        return data;
    }
}
